﻿#include <stdio.h>
#include <stdlib.h>
#include "uv.h"

uv_loop_t loop;

void print_net_devices(){
    uv_interface_address_t *info;
    int nCount = 0;
    char ipbuf[128];
    uv_interface_addresses(&info, &nCount);

    for(int i=0; i<nCount; i++){
        fprintf(stdout, "\n");
        fprintf(stdout, "Internal: %s\nName: %s\n", info[i].is_internal?"Yes":"No", info[i].name);
        fprintf(stdout, "IP Type: %s\n", info[i].address.address4.sin_family==AF_INET?"v4":"v6");
        if(info[i].address.address4.sin_family==AF_INET){
            uv_ip4_name(&info[i].address.address4, ipbuf, 128);
        }else{
            uv_ip6_name(&info[i].address.address6, ipbuf, 128);
        }
        fprintf(stdout, "IP: %s\n", ipbuf);
        fprintf(stdout, "MAC: %02X-%02X-%02X-%02X-%02X-%02X\n", info[i].phys_addr[0]&0xff,info[i].phys_addr[1]&0xff,
                info[i].phys_addr[2]&0xff,info[i].phys_addr[3]&0xff,info[i].phys_addr[4]&0xff,info[i].phys_addr[5]&0xff);
    }
    uv_free_interface_addresses(info, nCount);
}

int main(){
    printf("libuv demo!\n");

    print_net_devices();

    uv_loop_init(&loop);
    printf("Run uv loop\n");
    uv_run(&loop, UV_RUN_DEFAULT);

    uv_loop_close(&loop);

    return 0;
}
